package cn.test.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

import java.util.concurrent.ConcurrentHashMap;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *                 .' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >' "".
 *       | | : `- \`.;`\ _ /`;.`/ -` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * =============================================
 *          佛祖保佑             永无BUG
 *
 *
 * @Author: SJie
 * Date: 2020/8/4 12:51
 * Description: 
 *
 */

public class ServerHeartHandler extends ChannelInboundHandlerAdapter {
    ConcurrentHashMap<String, Integer> countMap = new ConcurrentHashMap();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        countMap.put(ctx.channel().id().asShortText(), 0);
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client exit ...");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        countMap.put(ctx.channel().id().asShortText(), 0);
        System.out.println(ctx.channel().remoteAddress() + "->server:" + msg.toString());
//        ctx.write(msg);
//        ctx.flush();
        super.channelRead(ctx, msg);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            switch (e.state()) {
                case READER_IDLE:
                    System.out.println("server read timeout,count: " + countMap.get(ctx.channel().id().asShortText()));
                    countMap.put(ctx.channel().id().asShortText(), countMap.get(ctx.channel().id().asShortText()) + 1);
                    if (countMap.get(ctx.channel().id().asShortText()) > 3) {
                        countMap.remove(ctx.channel().id().asShortText());
                        ctx.close();
                    }

                case WRITER_IDLE:
                    break;
                case ALL_IDLE:
                    break;
                default:
                    break;
            }
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
